package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.ui.chat.adapter.GroupContactListAdapter;
import com.hzcx.app.simplechat.ui.friend.FriendListByLableActivity;
import com.hzcx.app.simplechat.ui.friend.bean.GroupBean;
import com.hzcx.app.simplechat.ui.friend.bean.GroupListBean;
import com.hzcx.app.simplechat.ui.friend.contract.GroupListContract;
import com.hzcx.app.simplechat.ui.friend.presenter.GroupListPresenter;
import com.hzcx.app.simplechat.ui.group.GroupCreateActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GroupContactManageActivity extends BaseActivity<GroupListPresenter> implements GroupListContract.View, View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.ll_null)
    LinearLayout llNull;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.group_contract_list)
    ConstraintLayout groupContractList;
    @BindView(R.id.expandable_list)
    ExpandableListView listView;


    static String[] expandList = {"我创建的群聊", "我加入的群聊"};
    static List<List<GroupBean>> groupListBean = new ArrayList<>();

    @Override
    public int getLayout() {
        return R.layout.activity_manage_contact_group;
    }

    @Override
    public void initPresenter() {
        mPresenter = new GroupListPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(R.string.my_group);
        tvTitleRight.setText("发起群聊");
    }

    private void initExpandView() {
        final GroupContactListAdapter adapter = new GroupContactListAdapter(expandList, groupListBean);
        listView.setAdapter(adapter);
        // 清除默认的 Indicator
        listView.setGroupIndicator(null);
        //  设置分组项的点击监听事件
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.d(TAG, "onGroupClick: groupPosition:" + groupPosition + ", id:" + id);
                boolean groupExpanded = parent.isGroupExpanded(groupPosition);
                adapter.setIndicatorState(groupPosition, groupExpanded);
                // 请务必返回 false，否则分组不会展开
                return false;
            }
        });

        for(int i = 0; i < adapter.getGroupCount(); i++){
            listView.expandGroup(i);
        }
        //  设置子选项点击监听事件
        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//                Toast.makeText(GroupContactManageActivity.this, groupListBean.get(groupPosition).get(childPosition).getGroup_name(), Toast.LENGTH_SHORT).show();
                GroupBean groupBean = groupListBean.get(groupPosition).get(childPosition);
                // 是好友，直接聊天
                HxUserInfoBean hxUserInfoBean = new HxUserInfoBean();
                hxUserInfoBean.setHxusername(groupBean.getHx_groupid());
                hxUserInfoBean.setMember_id(Integer.valueOf(groupBean.getGroup_id()));
                hxUserInfoBean.setNickname(groupBean.getGroup_name());
                hxUserInfoBean.setAvatar(groupBean.getGroup_image());
//                hxUserInfoBean.setBy_remarks_name(data.getGroup_desc());
                hxUserInfoBean.setBy_remarks_name("");
                hxUserInfoBean.setRemarks_name(groupBean.getGroup_name());
                hxUserInfoBean.setMyUserId(UserInfoUtil.getUserId());
                hxUserInfoBean.setIs_assistant(0);
                ChatUtil.getInstance().toGroupChatUi(GroupContactManageActivity.this, hxUserInfoBean);
                return true;
            }
        });
    }

    @Override
    public void initData() {

    }


    @Override
    public void onStart() {
        super.onStart();
        mPresenter.getGroupList(this);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    @OnClick({R.id.tv_search, R.id.tv_title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_search:
                startActivity(new Intent(GroupContactManageActivity.this, GroupContactSearchActivity.class));
                break;
            case R.id.tv_title_right:
                newGroup();
                break;
        }
    }

    /**
     * 创建群组
     *
     */
    private void newGroup() {
        Intent intent = new Intent(this, GroupCreateActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(FriendListByLableActivity.INTENT_LIST_BUNDLE, null);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void groupListResult(GroupListBean data) {
        Log.i("groupListResult", "data = " + data.toString());

        if (data.getOwner().size() == 0 && data.getJoined().size() == 0) {
            llNull.setVisibility(View.VISIBLE);
            groupContractList.setVisibility(View.GONE);
            return;
        }
        llNull.setVisibility(View.GONE);
        groupContractList.setVisibility(View.VISIBLE);
        List<List<GroupBean>> groupList = new ArrayList<>();
        groupList.add(data.getOwner());
        groupList.add(data.getJoined());
        groupListBean = groupList;
        initExpandView();
    }

    @Override
    public void onFail() {
        Log.i("groupListResult", " fail");
    }
}
